package com.felix.ocoupon.advice;

import com.felix.ocoupon.exception.OcouponException;
import com.felix.ocoupon.vo.CommonResponse;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

/**
 * @author hsufelix
 * @email hsufelix@yeah.net
 * @Date 2020/11/11
 *
 * <h1>全局异常处理</h1>
 */
@RestControllerAdvice
public class GlobalExceptionAdvice {

    /**
     * <h2>对OcouponException进行统一处理</h2>
     * @param request 请求
     * @param ex 异常
     * @return CommonResponse<String>
     */
    @ExceptionHandler(value = OcouponException.class)
    public CommonResponse<String> handlerOcouponException(
            HttpServletRequest request, OcouponException ex
    ) {
        CommonResponse<String> response = new CommonResponse<>(
                -1, "ocoupon error"
        );
        response.setData(ex.getMessage());
        return response;
    }
}
